import question from "@/assets/json/questionList.js";
import questionDetailList from "@/assets/json/questionDetail.js";

export function getQuestionDetail(questionId) {
    var questionDetailArr = questionDetailList()
    var questionIdDetail = questionDetailArr.filter(item => parseInt(item.questionId) == questionId)
    return questionIdDetail
};

/**
 * 对象数组去重
 * @param {*} originalData 
 */
export const removeDuplicates = (originalData) => {
    let result, obj = {};
    result = originalData.reduce((cur, next) => {
        obj[next.key] ? "" : obj[next.key] = true && cur.push(next);
        return cur;
    }, []);
    return result;
};
/**
 * 获取浏览器url参数
 */
export const getRequest = function () {
    var url = location.href; //获取url中"?"符后的字串
    var theRequest = new Object();
    if (url.indexOf("?") != -1) {
        var str = url.split("?")[1];
        var strs = str.split("&");
        for (var i = 0; i < strs.length; i++) {
            theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
        }
    }
    return theRequest;
}
/**
 * 判定是否为一个对象
 */
export function isObject(par) {
    return typeof par === 'object' && par && typeof par !== 'function';
}

/**
 * 对比数组获取交集
 */
export function getArrIntersection(array1, array2) {
    var result = [];
    for (var i = 0; i < array2.length; i++) {
        var obj = array2[i];
        var num = obj.value;
        var isExist = false;
        for (var j = 0; j < array1.length; j++) {
            var aj = array1[j];
            if (aj == num) {
                isExist = true;
                break;
            }
        }
        if (isExist) {
            result.push(obj);
        }
    }
    return result
}
/**
 * 获取组织树信息
 */
export function getSectionArr() {
    var treeArr = JSON.parse(localStorage.getItem("organization"))
    var sectionArr = []
    for (const key in treeArr) {
        sectionArr.push(key);
    }
    return sectionArr
}
/**
 * 字符串日期转时间戳
 * 
 */
export function getTimeStamp(date) {
    var timestamp = Date.parse(date) / 1000
    return timestamp
}
/**
 * 数组去重
 */
export function objectRepeat(data) {
    var result = [];
    var obj = {};
    for (var i = 0; i < data.length; i++) {
        if (!obj[data[i].id]) {
            result.push(data[i]);
            obj[data[i].id] = true;
        }
    }
    return result
}
/**
 * 数组
 */
export function unique(arr) {
    if (!Array.isArray(arr)) {
        console.log('type error!')
        return
    }
    var array = [];
    for (var i = 0; i < arr.length; i++) {
        if (array.indexOf(arr[i]) === -1) {
            array.push(arr[i])
        }
    }
    return array;
}
/**
 * 
 * @param {} arr1 对比数组arr1是否包含arr2
 * @param {*} arr2 
 */
export function includes(arr1,arr2) {
    return arr2.every(val => arr1.includes(val));
}

/**
 * 根据题目的树状数据和步骤的id转化为具体的内容
 */

export function getSpecificName(nodeData,nodeId,questionId) {
    var questionName = ''
    var obj = getAllNode(nodeData, nodeId)
    var questionList = question()
    questionList.forEach(item=>{
        if(item.id === questionId){
            questionName=item.question
        }
    })
    obj.parentAllNode.shift()
    obj.parentAllNode.unshift(questionName)
    return [obj]
}
function setParentId(data) {
    data.nodeList.forEach(item => {
        item.parentId = data.id
    })
    return data.nodeList
}
function getAllNode(data, id) {//获取所有的节点操作
    var dataAllArr = []
    var obj = {}
    var fn = function (data) {
        data.forEach(item => {
            if (item.children) {
                dataAllArr = dataAllArr.concat(setParentId(item))
                fn(item.children)
            } else {
                dataAllArr = dataAllArr.concat(setParentId(item))
            }
        })
    }
    fn(data)
    dataAllArr.forEach(item => {
        if (item.nodeId === id) {
            obj = item
            obj.parentAllNode = treeFindPath(data, nodeData => nodeData.id === item.parentId)
        }
    })
    return obj
}
function treeFindPath(tree, func, path = []) {
    if (!tree) return []
    for (const data of tree) {
        // 这里按照你的需求来存放最后返回的内容吧
        path.push(data.title)
        if (func(data)) return path
        if (data.children) {
            const findChildren = treeFindPath(data.children, func, path)
            if (findChildren.length) return findChildren
        }
        path.pop()
    }
    return []
}
/**
 * 删除数组中指定的对象
 */
export function delArrayItem(array,item) {
    const index = array.findIndex(text => text.name === item.name);
    array.splice(index, 1);
    return array
}
/**
 * 删除指定数组中的值
 */
export function delArray(arr,str) {
    var index = arr.indexOf(str)
    arr.splice(index,1)
    return arr
}